package com.ruoyi.equipment.controller;

import com.hz.rouyigonggong.domain.basedomain.BaseDomain;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.equipment.domain.LgCamera;
import com.ruoyi.equipment.domain.LgColdStorage;
import com.ruoyi.equipment.domain.LgMachine;
import com.ruoyi.equipment.domain.LgSubCold;
import com.ruoyi.equipment.redis.RedisUtil;
import com.ruoyi.equipment.service.LgCameraService;
import com.ruoyi.equipment.service.LgMachineService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: lek
 * @Description: TODO
 * @Date: 2025/3/10 9:30
 * @Version: 1.0
 */
@RestController
@RequestMapping("/machine")
public class LgMachineController extends BaseController {
    @Autowired
    private LgMachineService lgMachineService;
    @Autowired
    private RedisUtil redisUtil;
    /**
     * @author maisan
     * @description //TODO 查询摄像头列表
     * @createTime  2025/3/7 19:07
     * @param
     * @return com.ruoyi.common.core.web.page.TableDataInfo
     **/

    @RequiresPermissions("equipment:coldThing:list")
    @GetMapping("/list")
    public TableDataInfo list(LgMachine lgMachine){
        startPage();
        lgMachine.setCreateBy(String.valueOf(BaseDomain.getUserId()));
        List<LgMachine> list = lgMachineService.selectLgMachineList(lgMachine);
        return getDataTable(list);
    }

    /**
     * @author maisan
     * @description //TODO 新增
     * @createTime  2025/3/7 19:08
     * @param
     * @return
     **/
    @PostMapping
    @RequiresPermissions("equipment:machine:add")
    public AjaxResult add(@Validated @RequestBody LgMachine lgMachine){
        lgMachine.setCreateBy(String.valueOf(BaseDomain.getUserId()));
        int i = lgMachineService.insertLgMachine(lgMachine);
        if (i == 0){
            return AjaxResult.error("新增失败");
        }
        return AjaxResult.success();
    }

    /**
     * @author maisan
     * @description //TODO 根据id查询
     * @createTime  2025/3/7 19:40
     * @param
     * @return
     **/
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Integer id){
        LgMachine lgMachine = lgMachineService.selectLgMachineById(id);
        return AjaxResult.success(lgMachine);
    }

    /**
     * @author maisan
     * @description //TODO 更新
     * @createTime  2025/3/7 19:42
     * @param
     * @return
     **/
    @PutMapping
    @RequiresPermissions("equipment:machine:edit")
    public AjaxResult update(@Validated @RequestBody LgMachine lgMachine){
        int i = lgMachineService.updateLgMachine(lgMachine);
        if (i == 0){
            return AjaxResult.error("修改失败");
        }
        return AjaxResult.success();
    }


    /**
     * @author maisan
     * @description //TODO 删除
     * @createTime  2025/3/7 19:38
     * @param
     * @return
     **/
    @DeleteMapping("/{ids}")
    @RequiresPermissions("equipment:machine:remove")
    public AjaxResult remove(@PathVariable Integer[] ids){
        int i = lgMachineService.deleteLgMachineById(ids);
        if (i == 0){
            return AjaxResult.error("删除失败");
        }
        return AjaxResult.success();
    }
    /**
     * @author maisan
     * @description 修改绑定信息
     * @createTime  2025/3/8 10:58
     * @param
     * @return
     **/
    @PutMapping("/updateBigCold")
    @RequiresPermissions("equipment:temperature:updateVehicle")
    public AjaxResult updateBigCold(@RequestBody Map<String, Object> map){
//        System.out.println(map.get("mid"));
//        System.out.println(map.get("subId"));
//        System.out.println(map.get("updateBy"));
        LgSubCold lgSubCold = lgMachineService.selectSubColdById((Integer) map.get("subId"));
        LgMachine lgMachine = new LgMachine();
        lgMachine.setLgSubCold(lgSubCold);
        List<LgMachine> lgMachineList = lgMachineService.selectLgMachineList(lgMachine);
        if (lgMachineList.size() == 0){
            lgMachine.setId((Integer) map.get("mid"));
            lgMachine.setColdStorageId((Integer) map.get("subId"));
            return AjaxResult.success(lgMachineService.updateLgMachine(lgMachine));
        }
        return AjaxResult.error("该设备已绑定");
    }

    /**
     * @author maisan
     * @description //TODO 取消绑定冷库
     * @createTime  2025/3/8 14:31
     * @param
     * @return
     **/
    @PutMapping("/delBound")
    @RequiresPermissions("equipment:machine:delBound")
    public AjaxResult delBound(@RequestBody Map<String, Object> map){
        LgMachine lgMachine = lgMachineService.selectLgMachineById((Integer) map.get("id"));
        lgMachine.setColdStorageId(0);
        return AjaxResult.success(lgMachineService.updateLgMachine(lgMachine));
    }
    @GetMapping("/macTemPic")
    @RequiresPermissions("equipment:machine:edit")
    public AjaxResult tempic(Integer id){
        Map<String,List> map  = new HashMap<>();
        map.put("temperature",redisUtil.getList("equipment:machine:temperature"+ id));
        map.put("humidity",redisUtil.getList("equipment:machine:humidity" + id));
        map.put("time",redisUtil.getList("equipment:machine:time" + id));
        return AjaxResult.success(map);
    }
}
